{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# about Generator\n",
    "\n",
    "\n",
    "## Introduction\n",
    "\n",
    "- 含有yield的函数，被调用时，会生成一个generator对象。注意，不是说这个函数是generator\n",
    "- 该对象的声明在最后一个yield后结束，也就是函数执行完毕。因此，这是一个走走停停的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'generator'>\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutGenerator/intro.py\n",
    "def gen_num():\n",
    "    yield 1\n",
    "    yield 2\n",
    "    yield 3\n",
    "s = gen_num()\n",
    "\n",
    "\n",
    "\n",
    "print(type(s))\n",
    "\n",
    "\n",
    "print(s.__next__())   #使用对象的方法\n",
    "print(next(s))        # 语法蜜糖，其实使用了上面的方法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## usually example\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1\n",
      "2 3 4 5 6 7 8 9 10 11 "
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-838926c53528>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-3-838926c53528>\u001b[0m in \u001b[0;36mgen_int\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mnum\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m         \u001b[0;32myield\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m         \u001b[0mnum\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;31m#自然数的源泉\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#%%writefile aboutGenerator/examp1.py\n",
    "def gen_int():\n",
    "    num = 0\n",
    "    while True:\n",
    "        yield num\n",
    "        num = num + 1\n",
    "#自然数的源泉\n",
    "s = gen_int()\n",
    "\n",
    "\n",
    "#取之不竭\n",
    "print(next(s),end=' ')\n",
    "print(next(s))\n",
    "\n",
    "n = 0\n",
    "for i in s:\n",
    "    if n<10:\n",
    "        print(i, end=' ')\n",
    "        n = n + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 list vs. generator\n",
    "\n",
    "\n",
    "- 注意下面的实例，两者的执行时间差别很大，尤其当列表的容量很大时，差别更为明显。\n",
    "- 在python3之后，range（）函数的调用返回的不再是list对象，而是一个generator。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 3.53 ms, sys: 283 µs, total: 3.81 ms\n",
      "Wall time: 3.77 ms\n",
      "<class 'list'>\n",
      "CPU times: user 5 µs, sys: 1 µs, total: 6 µs\n",
      "Wall time: 8.11 µs\n",
      "<class 'generator'>\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutGenerator/list2gen.py\n",
    "%time list1 = [i**2 for i in range(0,10000)]\n",
    "print(type(list1))\n",
    "\n",
    "%time gen1 = (i**2 for i in range(0,10000))\n",
    "print(type(gen1))\n",
    "\n",
    "#两者在使用上没有任何差别，但内存差异很大。\n",
    "def show(p):\n",
    "    for i in list1:\n",
    "        print(i,end=' ')\n",
    "    print('\\n')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
